package com.htgd.gardenexpo.controller;


import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConHealthy;
import com.htgd.gardenexpo.page.HealthyQuery;
import com.htgd.gardenexpo.service.GeConHealthyService;
import com.htgd.gardenexpo.service.WxUserCollectService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GeConHealthyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 健康科普 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/api/healthy")
@Api(tags = "健康科普")
public class GeConHealthyController {

    @Autowired
    private GeConHealthyService geConHealthyService;

    @Autowired
    private WxUserCollectService wxUserCollectService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    @Transactional
    public CommonResult add(@RequestBody GeConHealthy geConHealthy) {
        geConHealthy.setStatus(4);
        geConHealthyService.save(geConHealthy);
        return CommonResult.success("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult edit(@RequestBody GeConHealthy geConHealthy) {
        geConHealthyService.updateById(geConHealthy);
        return CommonResult.success("修改成功");
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete(String id) {
        geConHealthyService.removeById(id);
        return CommonResult.success("删除成功");
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConHealthyVO>> list(HealthyQuery healthyQuery, HttpServletRequest request) {
        PageInfo<GeConHealthyVO> list = geConHealthyService.pageList(healthyQuery,request);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }


    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeConHealthyVO> details(String id, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        GeConHealthyVO geConHealthyVO = new GeConHealthyVO();
        GeConHealthy geConHealthy = geConHealthyService.getById(id);
        GeConHealthy geConHealthy1 = new GeConHealthy();
        geConHealthy1.setId(id);
        geConHealthy1.setHit(geConHealthy.getHit()+1);
        geConHealthyService.updateById(geConHealthy1);
        GeConHealthy geConHealthy2 = geConHealthyService.getById(id);
        BeanUtils.copyProperties(geConHealthy2,geConHealthyVO);
        Boolean collect = wxUserCollectService.isLike(openId, geConHealthy.getId(),"healthy");
        if (collect) {
            geConHealthyVO.setIsLike(true);
        }else{
            geConHealthyVO.setIsLike(false);
        }
        return CommonResult.success("查询成功",geConHealthyVO);
    }

    @PostMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult review(@RequestBody GeConHealthy geConHealthy) {
        geConHealthyService.updateById(geConHealthy);
        return CommonResult.success("成功");
    }

}
